﻿using System.Collections.Generic;
using UnityEngine;
using VoxelBlock.VoxelBlock3D;
using VoxelBlock;using Vector3Int = VoxelBlock.Vector3Int;

public class OperationImplementLineRemove : OperationImplement
{
    IList<SimpleVoxel> oldSerializeVoxel = new List<SimpleVoxel>();
    Vector3Int rectangleOldPosition;


    //public OperationImplementLineRemove(ProxyOperation proxy) : base(proxy)
    //{

    //}

    public override void Start(StartArg arg)
    {
        base.Start(arg);

    }

    public override void Run(RunArg arg)
    {
        if (rectangleOldPosition != arg.nowSelectPosition)
        {
            Vector3Int positionA = arg.gestureStartPosition;
            rectangleOldPosition = arg.nowSelectPosition;

            var arrayPoint = BasicAlgorithm.GetLineBlock3D(positionA, rectangleOldPosition);
            Voxel3D[] addArray = new Voxel3D[oldSerializeVoxel.Count];
            for (int i = 0, max = oldSerializeVoxel.Count; i < max; i++)
            {
                SimpleVoxel voxel3d = oldSerializeVoxel[i];
                addArray[i] = new Voxel3D(voxel3d.position, voxel3d.uvKey, voxel3d.selectKey);
            }
            nowDrawVoxel3DCore.DrawAddBlock(addArray);
            oldSerializeVoxel = nowDrawVoxel3DCore.DrawRemoveBlock(arrayPoint);
        }
    }

    public override void Over(OverArg arg)
    {
        if (isStop)
        {
            if (oldSerializeVoxel.Count > 0)
            {
                proxy.Write(VoxelCategories.Operation.Remove, oldSerializeVoxel, historyRecordBreakList);
                //historyRecordFrontList = new List<SimpleVoxel>();
                oldSerializeVoxel = new List<SimpleVoxel>();
            }
        }
        else
        {
            if (oldSerializeVoxel.Count > 0)
            {
                proxy.Write(VoxelCategories.Operation.Remove, oldSerializeVoxel, historyRecordBreakList);
                //historyRecordFrontList = new List<SimpleVoxel>();
                oldSerializeVoxel = new List<SimpleVoxel>();
            }
            else
            {
                historyRecordFrontList = nowDrawVoxel3DCore.DrawRemoveBlock(new Vector3Int[] { arg.nowSelectPosition });
                if (historyRecordFrontList.Count > 0)
                {
                    proxy.Write(VoxelCategories.Operation.Remove, historyRecordFrontList, historyRecordBreakList);
                    historyRecordFrontList = new List<SimpleVoxel>();
                }
            }
        }
    }

}


